# 用拉链法构造哈希表

class LinkList:
    class Node:
        def __init__(self, item=None) -> None:
            self.item = item
            self.next = None
    
    class LinkListIterrator:
        def __init__(self, node) -> None:
            self.node = node
            
        def __next__(self):
            if self.node:
                cur_node = self.node
                self.node = cur_node.next 
                return cur_node.item
            else:
                raise StopIteration
            
        def __iter__(self):
            return self

    def __init__(self, iterable=None) -> None:
        self.head = None
        self.tail = None
        if iterable:
            self.extend(iterable)

    def append(self, obj):
        s = LinkList.Node(obj)
        if not self.head:   # 如果链表是空的
            self.head = s
            self.tail = s
        else:
            self.tail.next = s
            self.tail = s

    def extend(self, iterable):
        for obj in iterable:
            self.append(obj)

    def find(self, obj):
        for n in self:
            if n == obj:
                return True
            else:
                return False

    def __iter__(self):
        return self.LinkListIterrator(self.head)

    def __repr__(self):
        return '<<' + ', '.join(map(str, self)) + '>>'

class HashTable:
    def __init__(self, size=101) -> None:
        self.size = size
        self.T = [LinkList() for i in range(self.size)]

    def h(self, k):
        return k % self.size

    def insert(self, k):
        i = self.h(k)
        if self.find(k):
            print('Duplicated Insert.')
        else:
            self.T[i].append(k)

    def find(self, k):
        i = self.h(k)
        return self.T[i].find(k)

lyst = [1,2,3,4]
h = HashTable()
h.insert(lyst[0])
h.insert(lyst[1])
h.insert(lyst[2])
print(','.join(map(str, h.T)))